 function ghvec=plot_irfperturb(irf_mat,stpos,shockpos,legends,stnames,shonames,addtotit) 
% =========================================================================
% PLOT_IRFPERTURB.M 
%  
% ghvec=plot_irfperturb(irf_mat,stpos,shockpos,legends,stnames,shonames,addtotit) 
%
% Given a matrix of IRFS  IRFMAT of dimension [NST NX NPER+1 NPERT] 
% Where the pages correspond to periods and the books to parameter
% perturbations, plot the IRF to the states indicated in STPOS and to the
% shocks given in SHOCKPOS. Creating one figure for each SHOCK 
% 
% If STPOS (SHOCKPOS) not provided, whole matrix of states (shocks) will be produced 
% 
% Additional inputs 
% ------------------
% LEGENDS       Cell indicating which each line stands for 
% STNAMES       Cell with names of full length state vector (NSTx1) 
% SHONAMES      Cell with names of shocks (NX x 1 ) 
% ADDTOTIT      String to add to the SUPTITLE e.g. STORAGE 
% 
% Output
% GHVEC         Vector with the handles to each figure
%
% Alejandro Justiniano  January 30 2008 
% =========================================================================

% 1. Check dimensions for matrix and assign names to states, shocks and
% legends if not provided 
% =========================================================================
[nst nx nper nbook]=size(irf_mat);nper=nper-1; 
if nbook > 7;error('Only allow up to 7 perturbations in the same plot'); end 

if nargin < 2 || isempty(stpos);stpos=(1:nst)';end 
if nargin < 3 || isempty(shockpos);shockpos=(1:nx)';end
shockpos=sort(shockpos);nxuse=length(shockpos); 
if nxuse > nx; error('SHOCKPOS cannot be greater than NX'); end 
nstuse=length(stpos);
if nstuse > nst; error('STPOS cannot be greater than NST'); end 
    
% Check Other optional arguments 
% -------------------------------
if nargin < 4 || isempty(legends)
    legends=fnumcell('Line',(1:nbook));
else
    if length(legends)~=nbook;error('LEGENDS does not match books in IRF_MAT');end
end
if nargin < 5 || isempty(stnames)
    stnames=fnumcell('St',(1:nst));
else
    if length(stnames)~=nst;error('STNAMES does not match rows in IRF_MAT');end
end
if nargin < 6 || isempty(shonames);
    shonames=fnumcell('Shock ',(1:nx)); 
else 
    if length(shonames)~=nx;error('SHONAMES does not match columns in IRF_MAT'); end 
end 
if nargin < 7 || isempty(addtotit) 
    addtotit=''; 
else 
    addtotit=char(addtotit);  
end 

% 2. Define General Settings for the PLOTS 
% =========================================================================
style={'-','--',':','-','-*','--.',':*'}; 
width=[ 1.7  2.2   3  2    2   3     2  ]; 
colormatrix=[0.1    0.1     0.1
             0.35   0.35    0.35 
             0.55   0.55    0.55
             0.5    0.5     0.5 
             0.4    0.4     0.4 
             0.3    0.3     0.3 
             0.5    0.5     0.5 ]; 
ncol=2;
nrow=ceil((nstuse/2));
xvec=(0:nper)';
flag_roomleg=((nstuse/2-nrow)~=0); % Room for Legends in Extra Chart

if nrow < 5
    titfont=12; %Font for titles
elseif nrow < 8
    titfont=10;
else
    titfont=9;
end

% 3. One figure per shock, subfigure per state 
% =========================================================================
ghvec=zeros(nxuse);
for ii=1:nxuse;
    figure;
    % One figure for each STATE 
    for jj=1:nstuse
        subplot(nrow,ncol,jj);
        
        % Plot IRFS to each perturbation of the Impulse responses
        
        for kk=1:nbook;
            temp=squeeze(irf_mat(stpos(jj),shockpos(ii),:,kk));
%             if max( abs( temp )  ) < 1e-5
%                 temp=zeros( size(temp) );
%             end
            hand=plot(xvec,temp,style{kk},'LineWidth',width(kk));
            set(hand,'Color',colormatrix(kk,:));
            if kk==1;hold on;end
        end
        tempALL=squeeze(irf_mat(stpos(jj),shockpos(ii),:,:)); 
        
        ylim( limsaj( tempALL(:),0.05,[-0.01 0.01]) ); 
        hold off;
        title(stnames{stpos(jj)},'FontSize',titfont);
        set(gca,'box','off');
        xlim([xvec(1) xvec(end)+1]); 

    end
    % Put legend in new subplot if possible 
    % -------------------------------------
    if flag_roomleg==1
        subplot(nrow,ncol,jj+1);
        for kk=1:nbook;
            hand=plot(xvec,zeros(nper+1,1),style{kk},'LineWidth',width(kk));
            set(hand,'Color',colormatrix(kk,:));
            if kk==1;hold on;end
        end
        hold off; 
        ylim([0 10]);
        clear hand
    end
    hand=legend([220 10 0.4 0.4],legends,'orientation','horizontal','FontSize',...
        titfont+1);
    set(hand,'EdgeColor','w');
    clear hand
    ghvec(ii)=gcf;
    set(ghvec(ii),'PaperPosition',[0.75 0.5 7.5 10])

    hand=suptitle([shonames(shockpos(ii)),' ',addtotit]);
    set(hand,'FontSize',titfont+2);
end 